package com.ys.controller.web.kefu;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.entity.kefu.TkfKnow;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/kefu/tkfKnow")
public class WebTkfKnowController {
	
	private static Long MENU_ID = 67L;
	
	@Autowired
	private CommonApiService<TkfKnow> commonService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("tkfKnow:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			return commonService.toAdd(model, new TkfKnow(), MENU_ID, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 修改
	 */
	@RequiresPermissions("tkfKnow:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){

		try {
			return commonService.toEdit(model, id, new TkfKnow(), MENU_ID, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		TkfKnow entity = new TkfKnow().setId(id).queryFirst();
		
		String title = entity.getTitle();
		Integer type = entity.getType();
		String detail = entity.getDetail().replace("\n", "<br/>");
		ToDetailDto dto = new ToDetailDto(id, title, type, detail);
		System.out.println(dto.getDetail());
		model.addAttribute("dto", dto);
		return "kefu/tkfKnow/toDetail";
	}
	@Data
	@AllArgsConstructor
	private static class ToDetailDto{
		private Long id; // 
		private String title; // 标题
		private Integer type; // 类型
		private String detail; // 详情内容
	}
}
